var initdata = function (that) {
  var adress = that.data.adress
  for (var i = 0; i < adress.length; i++) {
    adress[i].shows = ""
  }
  that.setData({
    adress: adress
  })
}
const app = getApp()
Page({
  data: {
    delBtnWidth: 180, //删除按钮宽度单位（rpx） 
    adress: '',
    token: wx.getStorageSync('token'),
    uid: wx.getStorageSync('uid'),
  },

  onLoad: function (options) {
    this.getadress();
    this.initEleWidth();
  },

  onShareAppMessage: function () {

  },
  addadress: function () {  //跳转添加地址
    wx.navigateTo({
      url: '/pages/adress/addadress/addadress'
    })
  },
  getadress() {  //获取地址
    let _that = this;
    wx.request({   //传后台
      url: app.globalData.url + '/jxb/oneself/getAddressList',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid')
      },
      success: function (res) {
        _that.setData({
          adress: res.data.data
        });
        console.log(_that.data.adress)
      },

    })
  },
  touchS: function (e) {
    if (e.touches.length == 1) {
      this.setData({
        //设置触摸起始点水平方向位置 
        startX: e.touches[0].clientX
      });
    }
  },
  // 滑动中事件
  touchE: function (e) {
    if (e.changedTouches.length == 1) {
      //手指移动结束后水平位置 
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离 
      var disX = this.data.startX - endX;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮 
      var txtStyle = "";
      txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
      //获取手指触摸的是哪一项 
      var index = e.currentTarget.dataset.index;
      var adress = this.data.adress;
      adress[index].shows = txtStyle;
      // console.log("1", list[index].shows);
      //更新列表的状态 
      this.setData({
        adress: adress
      });
    } else {
      console.log("2");
    }
  },
  //获取元素自适应后的实际宽度 
  getEleWidth: function (w) {
    var real = 0;
    try {
      var res = wx.getSystemInfoSync().windowWidth;
      var scale = (750 / 2) / (w / 2);
      // console.log(scale); 
      real = Math.floor(res / scale);
      return real;
    } catch (e) {
      return false;
      // Do something when catch error 
    }
  },
  initEleWidth: function () {
    var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
    this.setData({
      delBtnWidth: delBtnWidth
    });
  },
  delItem: function (e) {  //删除
    var that = this;
    // 打印出当前选中的index  
    // 获取到列表数据
    var adress = that.data.adress;
    // 删除
    wx.showModal({
      title: '提示',
      confirmText: '好的',
      confirmColor: '#28A8F9',
      showCancel: true,
      content: '是否删除？',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '删除中...',
          })
          let id = e.currentTarget.dataset.index;
          wx.request({   //传后台
            url: app.globalData.url + '/jxb/oneself/delAddress',
            method: 'GET',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              token: wx.getStorageSync('token'),
              uid: wx.getStorageSync('uid'),
              id: id,
            },
            success: function (res) {
              if(res.data.code==0){
                wx.hideLoading()
                wx.showToast({
                  title: '已删除',
                  icon: 'success',
                  duration: 2000
                })
                that.getadress();
              }else{
                wx.showToast({
                  title: '网络错误',
                  icon: 'none',
                  duration: 2000
                })
              }            

            },
            fail:function(){
              wx.showToast({
                title: '网络错误',
                icon: 'none',
                duration: 2000
              })
            }

          })
        } else if (res.cancel) {
          return;
        }
      }
    })
  },
  edititem(e) {  //编辑
    var json = e.currentTarget.dataset.index;
    wx: wx.navigateTo({
      url: '/pages/adress/addadress/addadress?type=edit&json=' + JSON.stringify(json),
    })
  }
})